﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Input;

namespace ZGRemote.Server.UI.Controls
{
    public class IconTextBox : TextBox
    {
        public static RoutedCommand IconClick { get; } = new(nameof(IconClick), typeof(IconTextBox));

        public IconTextBox()
        {
            CommandBindings.Add(new CommandBinding(IconClick, (s, e) => OnIconClick(s, e)));
        }

        public object IconPath
        {
            get { return (object)GetValue(IconPathProperty); }
            set { SetValue(IconPathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconPath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconPathProperty =
            DependencyProperty.Register("IconPath", typeof(object), typeof(IconTextBox), new PropertyMetadata(null));




        public double IconWidth
        {
            get { return (double)GetValue(IconWidthProperty); }
            set { SetValue(IconWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconWidthProperty =
            DependencyProperty.Register("IconWidth", typeof(double), typeof(IconTextBox), new PropertyMetadata());



        public double IconHeight
        {
            get { return (double)GetValue(IconHeightProperty); }
            set { SetValue(IconHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconHeightProperty =
            DependencyProperty.Register("IconHeight", typeof(double), typeof(IconTextBox), new PropertyMetadata());


        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(IconTextBox), new PropertyMetadata(default(object)));

        private void OnIconClick(object s, ExecutedRoutedEventArgs e)
        {
            RaiseCloseButtonClickRoutedEvent();
            Command?.Execute(null);
        }

        // Register a custom routed event using the Bubble routing strategy.
        public static readonly RoutedEvent IconButtonClickEvent = EventManager.RegisterRoutedEvent(
            name: "IconButtonClick",
            routingStrategy: RoutingStrategy.Bubble,
            handlerType: typeof(RoutedEventHandler),
            ownerType: typeof(IconTextBox));

        // Provide CLR accessors for assigning an event handler.
        public event RoutedEventHandler IconButtonClick
        {
            add { AddHandler(IconButtonClickEvent, value); }
            remove { RemoveHandler(IconButtonClickEvent, value); }
        }

        void RaiseCloseButtonClickRoutedEvent()
        {
            // Create a RoutedEventArgs instance.
            RoutedEventArgs routedEventArgs = new(routedEvent: IconButtonClickEvent);

            // Raise the event, which will bubble up through the element tree.
            RaiseEvent(routedEventArgs);
        }


    }
}
